package file.login;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author hh
 * @since 2024/12/7
 */
public class TokenManager {

    private final static Logger log = LoggerFactory.getLogger(TokenManager.class);

    private static final Cache<String, String> cache = CacheBuilder.newBuilder()
            .expireAfterAccess(6, TimeUnit.HOURS)
            .build();

    public static void cacheToken(String token) {
        cache.put(token, "1");
        log.info("Token size: {}", cache.size());
    }

    public static String generateToken() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static boolean validateToken(String token) {
        return cache.getIfPresent(token) != null;
    }

}
